package com.shop.shard.controller.rest.user;

import com.shop.shard.common.*;
import com.shop.shard.controller.assembler.user.UserAssembler;
import com.shop.shard.controller.command.user.UserCreateCommand;
import com.shop.shard.controller.command.user.UserUpdateCommand;
import com.shop.shard.entity.user.User;
import com.shop.shard.vo.user.UserVO;
import com.shop.shard.service.user.UserService;
import com.shop.shard.manager.user.UserManager;
import com.shop.shard.mapper.user.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * @Description: 代码生成器自动生成
 * @author:
 * @Createed Date: 2019-8-16 10:06:57
 * @ModificationHistory: Who  When  What
 * ---------     -------------   --------------------------------------
 **/
@RestController
@RequestMapping("api/v1/user/user")
@Api(value = "userService", description = "user相关AIP")
public class UserRest  {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private UserManager userManager;


    @PostMapping("add")
    @ApiOperation(value = "add User ", notes = "xxx")
    public InvokeResult add(@RequestBody @Valid UserCreateCommand command) {
        User entity = UserAssembler.toUser(command);
        userService.addEntity(entity);
        return InvokeResult.success();
    }

    /**
    * delect User by id
    * @return
    */
    @PostMapping("delete")
    @ApiOperation(value = "delect User by id", notes = "xxx")
    public InvokeResult delete(Long id){
        userService.delete(id);
        return InvokeResult.success();
    }


    @PostMapping("update")
    @ApiOperation(value = "update User by id", notes = "xxx")
    public InvokeResult update(@RequestBody @Valid UserUpdateCommand command) {
        User entity = UserAssembler.toUser(command);
        userService.updateEntity(entity);
        return InvokeResult.success();
    }

    @GetMapping("detail")
    @ApiOperation(value = "get User detail by id", notes = "xxx")
    public InvokeResult detail(@RequestParam() Long id) {
        UserVO vo = userManager.selectDetailVoById(id);
        return InvokeResult.success(vo);
    }

    @GetMapping("page")
    @ApiOperation(value = "get User page ", notes = "xxx")
    public IPage<UserVO> page(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "10")Integer pageSize
                                ,String searchWord){
        Page page = new Page(pageNum,pageSize);
        Map<String,Object> map=new HashMap<>(4);
        if(searchWord != null){
            map.put("searchWord","%"+searchWord+"%");
        }
        IPage<UserVO> iPage=  userManager.getPage(page,map);
        return iPage;
    }
}
